<template>
    <div>
        <div>
            <el-col :span="3">
                <div class="grid-content bg-purple"></div>
                <el-button type="primary" @click="onhandAdd">添加衣物材质</el-button>
            </el-col>
            <el-input style="width: 240px;" placeholder="请输入衣物材质" v-model="params.clothesMaterialName"></el-input>
            <el-button style="margin-left: 10px;" type="primary" icon="el-icon-search " @click="loads">搜索</el-button>
            <el-button style="margin-left: 10px;" type="warning" icon="el-icon-scissors " @click="reset">重置</el-button>
        </div>
        <br>


        <div>
            <el-col :span="4" >
                <div class="grid-content bg-purple"></div>
                <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                               title="确认删除所有选中的衣物材质吗？" @confirm="deleteSelected">
                    <el-button type="danger" slot="reference"  v-bind:disabled="disable">删除所选</el-button>
                </el-popconfirm>
            </el-col>
        </div>
        <div>
            <el-col :span="6" style="margin-left: -38px">
                <div class="grid-content bg-purple"></div>
                <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="yellow"
                               title="确认保存所有更改吗？" @confirm="saveSelected">
                    <el-button type="warning" slot="reference">确认保存</el-button>
                </el-popconfirm>
            </el-col>
        </div>
        <el-table ref="multipleTable" :data="tableData" stripe style="width: 100%" tooltip-effect="dark"
                  @selection-change="handleSelectionChange">
        <el-table-column
                prop="clothesMaterialId"
                type="selection"
                width="55">
        </el-table-column>>
            <el-table-column prop="clothesMaterialName" label="衣物材质" width="180">
            </el-table-column>
            <el-table-column prop="washKindName" label="洗衣方式" width="180">
                <template v-slot="scope">
                    <el-select ref="selectwashKind" filterable v-model="scope.row.washKindName"
                               @change="saveSelect(scope.row.clothesMaterialId,scope.row.washKindName)">
                        <el-option
                                v-for="item in allWashKind"
                                :key="item.washKindId"
                                :label="item.washKindName"
                                :value="item.washKindId"
                        >
                        </el-option>
                    </el-select>
                </template>
            </el-table-column>

            <el-table-column label="操作" prop="clothesMaterialId">
                <template v-slot="scope">
                    <el-popconfirm confirm-button-text='确认' cancel-button-text='取消' icon="el-icon-info" icon-color="red"
                                   title="确认删除此会员种类吗？" @confirm="del(scope.row.clothesMaterialId)">
                        <el-button type="danger" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin-top: 20px">
            <el-button @click="toggleSelection()">取消选择</el-button>
        </div>
        <!-- 分页 -->
        <div style="margin-top: 10px;">
            <el-pagination background :current-page="params.pageNum" :page-size="params.pageSize"
                           layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
            </el-pagination>
        </div>


        <!-- 新增编辑弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :inline="true" :model="clothesMaterialform" status-icon :rules="rules" ref="clothesMaterialform"
                     style="width: 80%;"
                     label-width="200px">
                <el-form-item label="衣物材质" prop="clothesMaterialName">
                    <el-input v-model="clothesMaterialform.clothesMaterialName" placeholder="请输入衣物材质"></el-input>
                </el-form-item>
                <el-form-item label="洗衣方式" prop="washKindId">
                    <template>
                        <el-select v-model="clothesMaterialform.washKindId" filterable placeholder="请选择对应的洗衣方式">
                            <el-option
                                    v-for="item in allWashKind"
                                    :key="item.washKindId"
                                    :label="item.washKindName"
                                    :value="item.washKindId">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>

            </el-form>
                        <div style="text-align-last: center;width: 60%;">
                            <el-button @click="closedialog('clothesMaterialform')" type="danger">取 消</el-button>
                            <el-button @click="resetForm('clothesMaterialform')" type="warning">重置</el-button>
                            <el-button style="margin-right: 30px;" type="primary" icon="el-icon-upload2 "
                                       @click="dialogVisibleConfirm('clothesMaterialform')" size="medium">添加</el-button>
                        </div>
        </el-dialog>
    </div>
</template>

<script>
    import request from '@/request'

    export default {
        name: 'ClothesMaterial',
        data() {
            return {
                //修改过的数据存全局
                updateseleted:[],
                //选中的id存全局
                multipleSelection: [],
                tableData: [],
                allWashKind: [],
                washKind: '',
                total: 0,
                allid: "",
                disable:true,
                updatedisable:true,
                title: "添加衣物材质",
                dialogVisible: false,
                params: {
                    pageNum: 1,
                    pageSize: 8
                },
                clothesMaterialform: {
                    clothesMaterialName: '',
                    washKindId: ''
                },

                rules: {
                    clothesMaterialName: [{
                        required: true,
                        message: '衣物材质不能为空',
                        trigger: 'blur'
                    },],
                    washKindId: [{
                        required: true,
                        message: '洗衣方式不能为空',
                        trigger: 'blur'
                    },]

                }
            }
        },
        mounted() {
            this.getAllWashKind()
            this.load()
        },
        methods: {
            loads() {
                this.params.pageNum = 1
                this.load()
            },
            load() {
                request.get('/clothesmaterial/page', {
                    params: this.params
                }).then(res => {
                    if (res.code === "200") {
                        this.total = res.data.total;
                        this.tableData = res.data.list;
                    }
                })
            },
            getAllWashKind() {
                request.get('/washkind/getall').then(res => {
                    console.log(res)
                    if (res.code === "200") {
                        this.allWashKind = res.data;
                    }
                })
            },
            add(formName) {
                this.$refs[formName].validate((valid) => {

                    if (valid) {
                        request.post('/clothesmaterial/add', this.clothesMaterialform).then(res => {
                            if (res.code === '200') {
                                this.$notify.success('新增成功')
                                this.resetForm(formName) //清空表格数据
                                this.dialogVisible = false; //关闭弹框
                                this.load(); //刷新列表
                            } else {
                                this.$notify.error(res.msg)
                            }
                        })
                    }
                });
            },
            del(clothesMaterialId) {
                request.delete('/clothesmaterial/delete' + clothesMaterialId).then(res => {
                    if (res.code === '200') {
                        this.$notify.success('删除成功')
                        this.load();
                    } else {
                        this.$notify.error(res.msg)
                    }
                })

            },
            deleteSelected(){
                request.post('/clothesmaterial/deletes', this.multipleSelection).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('批量删除成功')
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            saveSelected(){
                request.put('/clothesmaterial/save', this.updateseleted).then(res => {
                    console.log(res)
                    if (res.code === '200') {
                        this.$notify.success('修改成功')
                        this.load(); //刷新列表
                    } else {
                        this.$notify.error(res.msg)
                    }
                })
            },
            reset() {
                this.params = {
                    user_name: '',
                    user_num: '',
                    pageNum: 1,
                    pageSize: 10
                }
                this.load()
            },
            saveSelect(clothesMaterialId,washKindId) {
                this.updateseleted.push({clothesMaterialId,washKindId})
                console.log(this.updateseleted)
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                if (val.length==0){
                    this.multipleSelection.length=val.length
                    this.disable=true
                }else {
                    this.disable=false
                    for (let i = 0; i < val.length ;i++) {
                        this.multipleSelection[i]=val[i].clothesMaterialId
                    }
                }

                console.log(this.multipleSelection)
            },
            handleCurrentChange(pageNum) {
                this.params.pageNum = pageNum;
                this.load()
            },
            handleClose(done) {
                this.dialogVisible = false;
            },
            onhandAdd() {
                this.resetForm('clothesMaterialform');
                this.dialogVisible = true;
            },
            dialogVisibleConfirm(formName) {
                // 新增接口
                    this.add(formName)
            },
            closedialog(formName) {
                this.dialogVisible = false;
                this.resetForm(formName) //清空表格数据
            },
            //清空表单
            resetForm(formName) {
                this.$nextTick(() => {
                    this.$refs[formName].resetFields();
                })
            },
        }
    }
</script>
